package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.StoreBo;
import com.xmtlxx.wms.domain.vo.StoreVo;
import com.xmtlxx.wms.service.IStoreService;
import com.xmtlxx.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 仓库Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "仓库控制器", tags = {"仓库管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/store")
public class StoreController extends BaseController {

    private final IStoreService iStoreService;

    private final ISysDeptService iSysDeptService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<StoreVo> initBean(){
        StoreVo vo = new StoreVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询仓库列表
     */
    @ApiOperation("查询仓库列表")
//    @SaCheckPermission("wms:store:list")
    @GetMapping("/list")
    public R<List<StoreVo>> list(@Validated(QueryGroup.class) StoreBo bo) {
        return R.ok(iStoreService.queryList(bo));
    }

    @ApiOperation("查询仓库列表")
//    @SaCheckPermission("wms:store:list")
    @GetMapping("/list/exclude/{id}")
    public R<List<StoreVo>> excludeChild(@ApiParam("仓库ID") @PathVariable(value = "id", required = false) String id) {
        List<StoreVo> items = iStoreService.queryList(new StoreBo());
        items.removeIf(c -> c.getId().equals(id) || ArrayUtil.contains(StringUtils.split(c.getAncestors(), ","), id + ""));
        return R.ok(items);
    }

    @ApiOperation("查询仓库下拉树列表")
    @GetMapping("/treeselect")
    public R<List<Tree<String>>> treeselect(StoreBo bo) {
        List<StoreVo> items = iStoreService.queryList(bo);
        return R.ok(iStoreService.buildStoreTreeSelect(items));
    }

    @ApiOperation("查询仓库下拉树列表")
    @GetMapping("/treeselectV2")
    public R<List<Tree<String>>> treeselectV2(StoreBo params) {
        if(LoginHelper.getLoginUser().getRolePermission().contains("sxcangguang")){
//            params.setDeptId(iSysDeptService.selectDeptById(getLoginUser().getDeptId()).getParentId());
            params.setStoreIds(iStoreService.selectAncestorsByUserId(LoginHelper.getUserId()));
        }
        List<StoreVo> items = iStoreService.treeStoreList(params);
        return R.ok(iStoreService.buildStoreTreeSelect(items));
    }


    @ApiOperation("查询仓库下拉树列表")
    @GetMapping("/treeselectAll")
    public R<List<Tree<String>>> treeselectAll(StoreBo bo) {
        List<StoreVo> items = iStoreService.queryListAll(bo);
        return R.ok(iStoreService.buildStoreTreeSelect(items));
    }

    @ApiOperation("查询仓库下拉树列表")
    @GetMapping("/appStoreList")
    public R<List<StoreVo>> appStoreList(StoreBo bo) {
        return R.ok(iStoreService.queryStoreList(bo));
    }
    /**
     * 导出仓库列表
     */
    @ApiOperation("导出仓库列表")
    @SaCheckPermission("wms:store:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated StoreBo bo, HttpServletResponse response) {
        List<StoreVo> list = iStoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库", StoreVo.class, response);
    }

    /**
     * 获取仓库详细信息
     */
    @ApiOperation("获取仓库详细信息")
//    @SaCheckPermission("wms:store:query")
    @GetMapping("/{id}")
    public R<StoreVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iStoreService.queryById(id));
    }

    /**
     * 新增仓库
     */
    @ApiOperation("新增仓库")
    @SaCheckPermission("wms:store:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreVo vo) {
        StoreBo bo = new StoreBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStoreService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改仓库
     */
    @ApiOperation("修改仓库")
    @SaCheckPermission("wms:store:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreVo vo) {
        StoreBo bo = new StoreBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStoreService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除仓库
     */
    @ApiOperation("删除仓库")
    @SaCheckPermission("wms:store:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iStoreService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
